package com.chenfan.process.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenfan.process.common.constants.CommonFieldConstants;
import com.chenfan.process.common.utils.QueryWrapperUtils;
import com.chenfan.process.entity.dto.ApprovalTemplateQueryDTO;
import com.chenfan.process.entity.po.ApprovalTemplatePo;
import com.chenfan.process.enums.BooleanEnum;
import com.chenfan.process.repository.ApprovalTemplateRepository;
import com.chenfan.process.service.ApprovalTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * @author K
 * @date 2021/3/17
 */
@Service
public class ApprovalTemplateServiceImpl extends ServiceImpl<ApprovalTemplateRepository, ApprovalTemplatePo>
    implements ApprovalTemplateService {

    @Autowired
    private ApprovalTemplateRepository approvalTemplateRepository;

    @Override
    public List<ApprovalTemplatePo> listAvailableApproval() {
        QueryWrapper<ApprovalTemplatePo> query = QueryWrapperUtils.newQueryWrapper();
        query.eq(ApprovalTemplatePo.Constants.TRANSFER_FLAG, BooleanEnum.Y.getCode());
        // TODO 删除
        return this.list(query);
    }

    @Override
    public List<ApprovalTemplatePo> listAvailableApproval(List<Long> approvalTemplateIdList) {
        QueryWrapper<ApprovalTemplatePo> query = QueryWrapperUtils.newQueryWrapper();
        query.in(CommonFieldConstants.ID, approvalTemplateIdList);
        return this.list(query);
    }

    @Override
    public Page<ApprovalTemplatePo> pageApprovalTemplate(Integer pageNum, Integer pageSize,
        ApprovalTemplateQueryDTO template) {
        Page<ApprovalTemplatePo> page = new Page<>(pageNum, pageSize);
        List<ApprovalTemplatePo> approvalTemplatePos = approvalTemplateRepository.pageApprovalTemplate(template, page);
        page.setRecords(approvalTemplatePos);
        return page;
    }

    @Override
    public ApprovalTemplatePo findByTitle(String title) {
        QueryWrapper<ApprovalTemplatePo> query = QueryWrapperUtils.newQueryWrapper();
        query.eq(ApprovalTemplatePo.Constants.TITLE, title);
        return this.getOne(query);
    }

    @Override
    public ApprovalTemplatePo getByCode(String code) {
        QueryWrapper<ApprovalTemplatePo> query = QueryWrapperUtils.newQueryWrapper();
        query.eq(ApprovalTemplatePo.Constants.CODE, code);
        return this.getOne(query);
    }

    @Override
    public List<ApprovalTemplatePo> listByCode(Collection<Long> code) {
        QueryWrapper<ApprovalTemplatePo> query = QueryWrapperUtils.newQueryWrapper();
        query.in(ApprovalTemplatePo.Constants.CODE, code);
        return this.list(query);
    }

    @Override
    public Integer statisticsNumber() {
        QueryWrapper<ApprovalTemplatePo> query = QueryWrapperUtils.newQueryWrapper();
        return this.count(query);
    }
}
